package com.example.demo.controller;

import com.example.demo.dto.Response;
import com.example.demo.entity.GoodsCategory;
import com.example.demo.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/beeorder/goodsCategory")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @GetMapping("/get/{id}")
    public GoodsCategory getById(@PathVariable Integer id) {
        GoodsCategory goodsCategory = goodsCategoryService.getById(id);
        return goodsCategory != null ? goodsCategory : new GoodsCategory();
    }

    @GetMapping("/get")
    public GoodsCategory getByEntity(GoodsCategory goodsCategory) {
        return goodsCategoryService.getByEntity(goodsCategory);
    }

    @GetMapping("/list")
    public Response<List<GoodsCategory>> list(GoodsCategory goodsCategory) {
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.listByEntity(goodsCategory);
        Response response = new Response();
        response.setCode(200);
        response.setData(goodsCategoryList);
        return response.SUCCESS(goodsCategoryList);
    }

    @PostMapping("/insert")
    public GoodsCategory insert(@RequestBody GoodsCategory goodsCategory) {
        goodsCategoryService.insert(goodsCategory);
        return goodsCategory;
    }

    @PutMapping("/update")
    public int update(@RequestBody GoodsCategory goodsCategory) {
        return goodsCategoryService.update(goodsCategory);
    }

    @DeleteMapping("/delete/{id}")
    public int deleteOne(@PathVariable Integer id) {
        return goodsCategoryService.deleteById(id);
    }

    @DeleteMapping("/delete")
    public int deleteBatch(@RequestBody List<Integer> ids) {
        int result = 0;
        if (ids != null && ids.size() > 0) result = goodsCategoryService.deleteByIds(ids);
        return result;
    }

}
